package com.ruoyi.sysem.controller;


import com.ruoyi.sysem.config.FastDFSClient;
import com.ruoyi.sysem.util.HttpUtils;
import com.ruoyi.sysem.util.ImageUtil;
import com.ruoyi.sysem.util.ResultObject;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UploadController {

    @Autowired
    private FastDFSClient fastDFSClient;

    /**
     * 上传到FastDFS上
     *
     * @param file
     * @return
     */
    @PostMapping("/uploadFastDFS")
    public ResultObject<Map> uploadFastDFS(@PathVariable MultipartFile file) throws IOException {
        String iconPath="D:\\study\\0df3d7ca7bcb0a469b7d75296463f6246b60af47.png";
        MultipartFile multipartFile = ImageUtil.addPicMarkToMutipartFile(file, iconPath);
        //MultipartFile multipartFile = ImageUtil.addWorkMarkToMutipartFile(file, "1111111111111111111111111111");
        //身份证
        String identityCard = fastDFSClient.upload(multipartFile);
        Map map = new HashMap();
        map.put("identityCard", identityCard);
        System.out.println("11111");
        return new ResultObject<>(true, "1", "长传成功", map);
    }


    public static void main(String[] args) {
        String host = "https://jumjokk.market.alicloudapi.com";
        String path = "/personal/disenforcement";
        String method = "POST";
        String appcode = "fb91964ff8864e639dc3769d5d50b1ed";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("idcard_number", "130828200104252014");
        bodys.put("mobile_number", "18034064225");
        bodys.put("name", "张硕");


        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            System.out.println("11111111"+EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
